import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

import javax.jmdns.JmDNS;
import javax.jmdns.ServiceEvent;
import javax.jmdns.ServiceInfo;
import javax.jmdns.ServiceListener;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.text.ParseException;
import java.text.SimpleDateFormat;

public class ServiceTicket {
	
    private String SERVICE_TYPE_CREATE = "AutoCheckIn._udp.local.";
	private String SERVICE_NAME = "AutoCheckIn";
	private static int SERVICE_PORT = 1268;
	private ServerSocket my_serverSocket;
	static HashMap<Integer,Passenger> passengers;

	ServiceTicket(int port){
		try
        {
			JmDNS jmdns = JmDNS.create();
	    	ServiceInfo info = ServiceInfo.create(SERVICE_TYPE_CREATE, SERVICE_NAME, SERVICE_PORT, 0, 0,"a_property=some_value");
	    	jmdns.registerService(info);
			my_serverSocket = new ServerSocket(port);
            System.out.println("Server waiting for client on port " + my_serverSocket.getLocalPort());
            while(true)
                {
                        Socket socket = my_serverSocket.accept();  
                        System.out.println("New client asked for a connection");
                        TcpThread t = new TcpThread(socket);    
                        System.out.println("Starting a thread for a new Client");
                        t.start();
                }
        }
        catch (IOException e) {
                System.out.println("Exception on new ServerSocket: " + e);
        }
        
	}
	public static void main(String[] arg) {
		 passengers = new HashMap<Integer,Passenger>();
		 passengers.put(0,new Passenger(12345,"2009-11-11 23:30","G",false,"John_Peterson","British_airways"));
		 passengers.put(1,new Passenger(54321,"2009-12-03 12:30","A",false,"Patty_Lewis","Ryanair"));
		 passengers.put(2,new Passenger(00001,"2009-01-12 16:40","S",false,"Dave_Smith","American_airlines"));
         new ServiceTicket(SERVICE_PORT);
	}
	class TcpThread extends Thread {
    	public static final String DATE_FORMAT = "yyyy-MM-dd HH:mm";
        Socket socket;
        TcpThread(Socket socket) {
                this.socket = socket;
        }
        public String getTime(String newDate,int index,boolean state){
		     Calendar cal = Calendar.getInstance();
		     SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
		     Date d = null;
		     try {
		    	 d = sdf.parse(newDate);
		     } catch (ParseException e) {
		    	 e.printStackTrace();
		     }
			Calendar newCal = Calendar.getInstance();
			newCal.setTime(d);
			if (cal.before(newCal)){
			  long temp = (newCal.getTimeInMillis()-cal.getTimeInMillis());
			  if(temp < 86400000){
				  if(passengers.get(index).getStatus()!=true){
					  passengers.get(index).Change_state(false);
					  return "Yes "+temp+" "+passengers.get(index).getGate()+" "+passengers.get(index).get_Name()+" "
					  +passengers.get(index).get_flight()+" "+passengers.get(index).getDetails();
				  }else{
					  return "No "+temp+" "+passengers.get(index).getGate()+" "+passengers.get(index).get_Name()+" "
					  +passengers.get(index).get_flight()+" "+passengers.get(index).getDetails();
				  }
			  }else return "Your are more than a day early for your flight, we are unable to register you";
			}
			return "You have missed your flight";
      }
        public void run() {
			try {
				BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                String message = in.readLine();
				PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
				int missing = 0;
				for(int i=0;i<passengers.size();i++){
					if(Integer.parseInt(message)==passengers.get(i).getReg()){
						String temp = getTime(passengers.get(i).getDetails(),i,passengers.get(i).getStatus());
						passengers.get(i).Change_state(true);
						out.println(temp);
						System.out.println(temp);
					}else{
						missing++;
					}
				}
				if(missing==3){
					out.println("Passenger not found.");
					System.out.println("Not found.");
				}
		   } catch (IOException e) {
				e.printStackTrace();
			}
                
        }
	}
	
}

